package summary;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @Author: 海琳琦
 * @Date: 2022/7/6 20:46
 * https://leetcode.cn/problems/next-greater-element-i/
 */
public class Title496 {

    public int[] nextGreaterElement(int[] nums1, int[] nums2) {
        Map<Integer, Integer> hash = new HashMap<>();
        int n = nums1.length;
        for (int i = 0; i < n; i++) {
            hash.put(nums1[i], i);
        }
        int[] result = new int[n];
        Arrays.fill(result, -1);
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums2.length; i++) {
            while (!stack.isEmpty() && nums2[i] > nums2[stack.peek()]) {
                Integer left = stack.pop();
                if (hash.containsKey(nums2[left])) {
                    result[hash.get(nums2[left])] = nums2[i];
                }
            }
            stack.push(i);
        }
        return result;
    }

    public static void main(String[] args) {

    }
}
